package com.dayaokeji.rhythmschoolstudent.utils;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.dayaokeji.rhythmschoolstudent.client.common.base.App;
import com.dayaokeji.rhythmschoolstudent.client.common.base.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yanzhenjie.permission.AndPermission;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Gorden on 2017/10/30.
 */

public class WifiUtil {

    public static final String COURSE_WIFI_CACHE_KEY = "course_wifi_cache";
    public static final String MEETING_WIFI_CACHE_KEY = "meeting_wifi_cache";

    public static final int WIFI_CACHE_EXPIRED_TIME = 1 * 60;

    private static Gson gson = new Gson();

    /**
     * 过滤wifi列表信息
     * @param scanResults
     * @return
     */
    private static List<String> filterWifiResult(@NonNull List<ScanResult> scanResults) {
        List<String> wifis = new ArrayList<>();
        final String connectWifiMacAddr = AndroidUtils.connectWifiMacAddr();
        if (!TextUtils.isEmpty(connectWifiMacAddr)) {
            wifis.add(connectWifiMacAddr);
        }
        if (!scanResults.isEmpty()) {
            for (ScanResult scanResult : scanResults) {
                wifis.add(TextUtils.isEmpty(scanResult.BSSID)? "" : scanResult.BSSID.replace(":","").toUpperCase());
            }
        }
        return wifis;
    }

    public static String connectionAddress() {
        return AndroidUtils.connectWifiMacAddr();
    }

    @NonNull
    public static String wifiListJson(@NonNull List<String> wifiList) {
        return gson.toJson(wifiList);
    }

    @Nullable
    public static List<String> wifiList(@NonNull String wifiListJson) {
        final Type type = new TypeToken<List<String>>(){}.getType();
        return gson.fromJson(wifiListJson, type);
    }

    public static Observable<List<String>> getWifiList() {
        return Observable.create(new ObservableOnSubscribe<List<ScanResult>>() {
            @Override
            public void subscribe(ObservableEmitter<List<ScanResult>> emitter) throws Exception {
                emitter.onNext(scanWifiList());
            }
        }).map(new Function<List<ScanResult>,List<String>>() {
            @Override
            public List<String> apply(List<ScanResult> scanResults) throws Exception {
                return filterWifiResult(scanResults);
            }
        }).observeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 获取wifi列表
     *
     * @return
     */
    private static List<ScanResult> scanWifiList() {
        try{
            if (AndPermission.hasPermissions(App.getAppContext(), Constant.SIGN_PERMISSION_GROUP)) {
                WifiManager wifiManager = (WifiManager) App.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                if (wifiManager == null) return Collections.emptyList();
                if (!wifiManager.isWifiEnabled()) {
                    wifiManager.setWifiEnabled(true);
                }
                return wifiManager.getScanResults();
            }else{
                return Collections.emptyList();
            }
        }catch (Exception e){
            return Collections.emptyList();
        }
    }


    /**
     * 检查当前会议地址在不在当前wifi列表中
     *
     * @return
     */
    public static boolean checkMacAddress(@Nullable String mck,@Nullable List<String> wifiList) {
        if (mck != null && !TextUtils.isEmpty(mck) && wifiList != null) {
            String[] netMcks = mck.split(";");
            for (String netMck : netMcks) {
                for (String wifi : wifiList) {
                    if (ObjectUtils.equals(netMck, wifi)) {
                        return true;
                    }
                }
            }
            return false;
        } else {
            return false;
        }
    }
}
